﻿using System;
using System.IO;
using System.Text;

namespace JianZhangTool.log
{
    public class Logger
    {
        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="log_type"></param>
        /// <param name="log_str"></param>
        public static void log(string log_type, string log_str)
        {
            if (Main.Instance.GetRoomGuardInfos() == null)
            {
                return;
            }
            Main.Instance.GetRoomGuardInfos().logger.add_log(log_type, log_str);
        }

        /// <summary>
        /// 原始房间号
        /// </summary>
        private int origin_roomId;
        /// <summary>
        /// 日志文件夹路径
        /// </summary>
        private string path;

        public Logger(int origin_roomId)
        {
            this.origin_roomId = origin_roomId;
            path = Environment.CurrentDirectory + @"\log\" + origin_roomId + @"\";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="log_type"></param>
        /// <param name="log_str"></param>
        private void add_log(string log_type, string log_str)
        {
            string file_path = path + log_type + @"\";
            if (!Directory.Exists(file_path))
            {
                Directory.CreateDirectory(file_path);
            }
            string file_name = file_path + DateTime.Now.ToString("yyyy-MM-dd") + ".log";

            StreamWriter sw = new StreamWriter(file_name, true, Encoding.UTF8);
            if (log_type.Equals("test"))
            {
                sw.Write(log_str);
            }
            else
            {
                sw.WriteLine(log_str);
            }
            sw.Flush();
            sw.Close();
        }
    }
}
